//入口js文件
console.log('Hello World!');
import Vue from 'vue'//导入模块
import VueRouter from 'vue-router';
import MintUI from 'mint-ui';
import axios from 'axios';
import VueAxios from "vue-axios";
import ElementUI from 'element-ui'
import moment from "moment";
import mui from './lib/mui-master/dist/js/mui.js';
import app from './App.vue';//导入组件
import ViewPreview from 'vue-preview'
import Vuex from 'vuex'

import 'mint-ui/lib/style.min.css';//导入依赖
import './lib/mui-master/dist/css/mui.min.css';
import './lib/mui-master/dist/css/icons-extra.css';
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(MintUI);
Vue.use(VueRouter);
Vue.use(ViewPreview);
Vue.use(Vuex);
Vue.use(ElementUI);
axios.defaults.baseURL = 'http://www.liulongbin.top:3005/api/';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
Vue.use(VueAxios, axios);
//定义全局的日期过滤器
Vue.filter('dateFormat', function (dateStr, pattern="YYYY-MM-DD HH:mm:ss") {
    return moment(dateStr).format(pattern);
});

import router from './router'//导入项目的路由

let car = window.JSON.parse(localStorage.getItem('car')||'[]');

let store = new Vuex.Store({
    state: {
        //购物车中的数据{id: 商品的id, count: 要购买的数量, price: 商品的单价, selected: 商品是否被选中，maxCount: 商品的最大数量}
        car,
        maxForId: {},
        totalForId: {},
        isTrueForId: {}
    },
    mutations: {
        addToCar(state, goodsInfo) {
            let flag = false;//假设没有找到对应的商品
            state.car.some(item => {
                if (item.id === goodsInfo.id) {
                    item.count = parseInt(goodsInfo.count) + parseInt(item.count);
                    flag = true;
                    return true;
                }
            });
            if (!flag) {
                state.car.push(goodsInfo);
            }
            //写全名称
            localStorage.setItem('car', window.JSON.stringify(state.car));
            console.log(state.car)
        },
        getMaxForId(state) {
            state.car.forEach(item => {
                state.maxForId[item.id] = item.maxCount;
            });
        },
        getTotalForId(state, o) {
            state.totalForId = o;
            state.car.forEach(item => {
                for (let key in state.totalForId) {
                    if (item.id === key) {
                        item.count = parseInt(state.totalForId[key]);
                    }
                }
            });
            localStorage.setItem('car', window.JSON.stringify(state.car));
        },
        getIsTrueForId(state, o) {
            state.isTrueForId = o;
            state.car.forEach(item => {
                for (let key in state.isTrueForId) {
                    if (item.id === key) {
                        if (state.isTrueForId[key] === false) {
                            item.count = 0;
                        } else {
                            item.count = state.totalForId[item.id]
                        }
                    }
                }
            });
            localStorage.setItem('car', window.JSON.stringify(state.car));
        },
        deleteById(state, id) {
            state.car.some((item, i) => {
                if (parseInt(item.id) === id) {
                    state.car.splice(i, 1);
                    return true;
                }
            });
            localStorage.setItem('car', window.JSON.stringify(state.car));
        },
    },
    getters: {
        getAllCount(state) {
            let allCount = 0;
            state.car.forEach(item => {
                allCount += parseInt(item.count);
            });
            return allCount;
        },
        getMaxForId(state) {
            return state.maxForId;
        },
        getTotalPrice(state) {
            let totalPrice = 0;
            state.car.forEach(item => {
                if (state.isTrueForId[item.id] === true) {
                    totalPrice = totalPrice + state.totalForId[item.id]*item.price;
                }
            })
            return totalPrice;
        }
    }

});

let vm = new Vue ({
    el: '#app',
    render: e => e(app),
    router,
    store
});

